package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 东方网解析
 * @author Persh
 *
 */
public class EastdayNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
	"http://.*.eastday.com/.*/[0-9]{8}/[^.]*.html",
	"http://.*.eastday.com/.*[.]asp[?]pkey=[0-9]*",
	"http://.*.eastday.com/.*[.]aspx[?]id=[0-9]*",
	"http://.*.eastday.com/.*/[0-9]{4}[-][0-9]{2}[-][0-9]{2}/[0-9]*.html",
	"http://.*.eastday.com/.*/.*/.*.aspx[?]QuestionID=[0-9]*&page=[0-9]*"};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		doc.select("#sectionright").remove();
		doc.select(".xgxw").remove();
		doc.select("a[href] > strong").remove();
		doc.select("#pageindex").remove();

			title =doc.select("h1").text();
			if(title==null || title.trim().length()==0){
				title =doc.select("#biaoti").text();
					if(title==null || title.trim().length()==0){
						title =doc.select("td.n16").text();
						if(title==null || title.trim().length()==0){
							title =doc.select("table#ctl00_ContentPlaceHolder1_dateListTopic>tr>td>div>span.hui14a").text();
							if(title==null || title.trim().length()==0){
								title =doc.select("div.sectionleft2>ul.lb13>li.blue18b").text();
								if(title==null || title.trim().length()==0){
									title =doc.select("span.hui18").text();
									if(title==null || title.trim().length()==0){
										title =doc.select("div#left.fl>h1.blue22").text();
										if(title==null || title.trim().length()==0){
											title =doc.select("div#sectionleft1.fl>div.tx2>div.fc").text();
											if(title==null || title.trim().length()==0){
												title =doc.select("div#content>div.left19>div.block8>div.tx44>h1.pix24h2").text();
												if(title==null || title.trim().length()==0){
													title =doc.select("span.STYLE9").text();
													if(title==null || title.trim().length()==0){
														title =doc.select("div.title").text();
														if(title==null || title.trim().length()==0){
															title =doc.select("span.hui14a").text();
														}
													}
												}
											}
										}
									}
								}
							}
					}
				}
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("div.text>div.clear"));
			if(content==null || content.trim().length()==0){
				content=HtmlCleaner.getContentHtml(url,doc.select("div#zw"));
				if(content==null || content.trim().length()==0){
					content=HtmlCleaner.getContentHtml(url,doc.select("ul#zw"));
					if(content==null || content.trim().length()==0){
						content=HtmlCleaner.getContentHtml(url,doc.select("td.n14"));
						if(content==null || content.trim().length()==0){
							content=HtmlCleaner.getContentHtml(url,doc.select("table#ctl00_ContentPlaceHolder1_dateListTopic>tr>td>table>tr"));
							if(content==null || content.trim().length()==0){
								content=HtmlCleaner.getContentHtml(url,doc.select("td.hui14a"));
								if(content==null || content.trim().length()==0){
									content=HtmlCleaner.getContentHtml(url,doc.select("div#sectionleft1.fl>div.tx2>div.lh28"));
									if(content==null || content.trim().length()==0){
										content=HtmlCleaner.getContentHtml(url,doc.select("div#content>div.left19>div.block8>div.tx44>div.pix14a4"));
										if(content==null || content.trim().length()==0){
											content=HtmlCleaner.getContentHtml(url,doc.select("td.STYLE9"));
											if(content==null || content.trim().length()==0){
												content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
											}
										}
									}
								}
							}
						}
					}
				}
			}
			content=content.replace("正在加载中... ", "");

			date=DateUtils.matchDate(doc.select("div.text>h2").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("div#left>h2").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("p.cen").text());
					if(date==null){
						date=DateUtils.matchDate(doc.select("h2.fc").text());
						if(date==null){
							date=DateUtils.matchDate(doc.select("td.h12>div>span.hui14a").text());
							if(date==null){
								date=DateUtils.matchDate(doc.select("table#ctl00_ContentPlaceHolder1_dateListTopic>tr>td>table>tr>td.hui14a").text());
								if(date==null){
									date=DateUtils.matchDate(doc.select("div#sectionleft1.fl>div.tx2>div.tx4>p.grey12b").text());
									if(date==null){
										date=DateUtils.matchDate(doc.select("div.sectionleft2>ul.lb13>li.cen").text());
										if(date==null){
											if(doc.select("html>body>table>tr>td>table>tr>td>table>tr>td>table>tr").size()>2){
												date=DateUtils.matchDate(doc.select("html>body>table>tr>td>table>tr>td>table>tr>td>table>tr").get(1).text());
											}
											if(date==null){
												date=DateUtils.matchDate(doc.select(".fc").text());
												if(date==null){
													date=DateUtils.matchDate(doc.select(".time").text());
													if(date==null){
														date=DateUtils.matchDate(doc.select("div.title_x").text());
														if(date==null){
															date=DateUtils.matchDate(doc.select("td.hui14a").text());
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setDate(date);
		String author=doc.select("div#section div#sectionleft div.time p a").html();

		news.setAuthor(author);
		return news;
	}

	public static void main(String[] args) {
		EastdayNewsAnalyse analyse=new EastdayNewsAnalyse();
		String url="http://wmzx.eastday.com/channel_bbs/view.aspx?id=44218";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
